package com.semidata.rtd.core.iplib;

public class Area {

    private int country;
    private int province;
    private int city;
    private String code;

    public Area(String code) {
        if (code == null || code.length() != 10)
            throw new RuntimeException("invalid area, code = " + code);
        try {
            country = Integer.parseInt(code.substring(0, 4));
            province = Integer.parseInt(code.substring(4, 6));
            city = Integer.parseInt(code.substring(6, 8));
        } catch (Exception e) {
            throw new RuntimeException("invalid area, code = " + code);
        }
        
        if ((country == 0 && (province != 0 || city != 0)) || (province == 0 && city != 0))
            throw new RuntimeException("invalid area, code = " + code);
        this.code = code;
    }

    public boolean contains(Area area) {
        return (country == 0 || country == area.country) && (province == 0 || province == area.province) && (city == 0 || city == area.city);
    }
    
    public String toString() {
        return code;
    }
    
    public int hashCode() {
        return code.hashCode();
    }
    
    public boolean equals(Object obj) {
        if (obj instanceof Area) {
            return code.equals(((Area)obj).code);
        }
        return false;
    }
}
